import pytest
import requests
from common.request_handle import mylog
import warnings
from urllib3.exceptions import InsecureRequestWarning


@pytest.hookimpl(optionalhook=True)
def pytest_html_report_title(report):
    report.title = "东高二期自动化测试报告"


@pytest.fixture(scope='session')
def get_drivers():
    warnings.simplefilter('ignore', InsecureRequestWarning)
    mylog.info('==============================接口自动化开始===============================')
    url = 'https://dxuas.wehdz.gov.cn:30698' + "/gdu-unified-portal-api/auth/oauth2/token?grant_type=password&username=" + 'zysall' + "&scope=server&randomStr=blockPuzzle&code=EZXohniJiQstVhIQEblJpHsVVP33VvQd6pzO5jAq%2FSl0QoZyRWu3JwYM0190WGWPjX6yWtKwsHPTbaMB0HXqzg%3D%3D"
    payload = 'password=' + 'YGHMPDFkCMT8Tg=='
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': 'Basic Z2R1LXVhcC11cG1zOmdkdS11YXAtdXBtcw==',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
    }
    response = requests.request("POST", url, headers=headers, data=payload, verify=False)
    res = response.json()
    access_token = res["data"]["access_token"]
    mylog.info(F'登录成功：{access_token}')
    yield access_token
    mylog.info('==============================接口自动化结束===============================')

def Client_Driver():
    warnings.simplefilter('ignore', InsecureRequestWarning)
    mylog.info('==============================接口自动化开始===============================')
    url = "http://dxuas.wehdz.gov.cn:30790/api/front/user/auth/login?username=16620167546&grantType=password&scope=server&userType=0"
    payload = 'password=YHDKbG87BMny'
    headers = {
        'Accept': ' application/json, text/plain, */*',
        'Authorization': ' Basic bGEtd2Vic2l0ZS1wb3J0YWw6bGEtd2Vic2l0ZS1wb3J0YWw=',
        'Connection': ' keep-alive',
        'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
        'Cookie': ' Tenant-Id=1; token=4c65f8a6-07fe-4e83-adad-db54247ce8b3',
        'Content-Type': ' application/x-www-form-urlencoded',
        'Host': 'dxuas.wehdz.gov.cn:30790'
    }
    response = requests.request("POST", url, headers=headers, data=payload, verify=False)
    res = response.json()
    access_token = res["data"]["access_token"]
    mylog.info(F'登录成功：{access_token}')
    yield access_token
    mylog.info('==============================接口自动化结束===============================')